<template>
  <div>
    <!-- <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">创建日期</span>
          <el-date-picker
            v-model="createdateList"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          ></el-date-picker>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
        </div>
      </el-col>
    </el-row>-->
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">广告标题</span>
          <el-input placeholder="请输入标题" v-model="param.banner.name"></el-input>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item">
          <span class="title">广告位</span>
          <el-select v-model="param.banner.type" style="width:100%" placeholder="请选择">
            <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="hadleAddUser" icon="el-icon-circle-plus-outline">新增广告</el-button>
          <!-- <el-button type="primary" @click="addAlliance" icon="el-icon-circle-plus-outline">植入商盟广告</el-button> -->
        </div>
      </el-col>
    </el-row>
    <!-- <el-container class="add-contianer">
      <el-button class="add-btn" @click="hadleAddUser" size="mini" type="primary">新增广告轮播</el-button>
    </el-container>-->
    <div class="table-body">
      <el-table :data="tableData" v-loading="isLoading" border style="width: 100%">
        <el-table-column prop="sort" label="序号" width="80"></el-table-column>
        <el-table-column prop="name" label="广告标题"></el-table-column>
        <el-table-column prop="url" label="广告图片">
          <template slot-scope="scope">
            <template>
              <el-image class="banner-image" fit="cover" :src="scope.row.url"></el-image>
              <div v-show="!scope.row.disabled"></div>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="typesName" label="广告位"></el-table-column>
        <el-table-column prop="spuTitle" label="绑定spu商品"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small">修改</el-button>
            <el-popconfirm title="确定删除此广告？" @onConfirm="handleConfirmDelete(scope.row)">
              <el-button style="margin-left:15px" type="danger" size="small" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
      <el-dialog title="新增广告" :visible.sync="isVisible" width="40%">
        <add-ad @submit="submitevent" v-if="isVisible" />
      </el-dialog>
      <el-dialog title="修改广告" :visible.sync="isVisible1" width="40%">
        <updateAd :data="adrow" @submit="updateEvent" v-if="isVisible1" />
      </el-dialog>

      <!-- <el-dialog title :visible.sync="DialogVisiblejj" width="42%">
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item label="广告位" prop="type" :label-width="formLabelWidth">
            <el-select v-model="form.type" style="width:100%" placeholder="请选择">
              <el-option
                v-for="item in productOptions"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商盟广告列表" prop="Adlist" :label-width="formLabelWidth">
            <el-select v-model="form.Adlist" style="width:100%" placeholder="请选择">
              <el-option
                v-for="item in productOption"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="explain" :label-width="formLabelWidth">
            <el-input type="textarea" :rows="3" placeholder="请填写" v-model="form.explain"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth">
            <el-button plain @click="resetForm('formRef')">取消</el-button>
            <el-button type="primary" @click="submitForm('formRef')">提交</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>-->
    </div>
  </div>
</template>

<script>
import AddAd from "./AddAd";
import updateAd from "./update";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      isLoading: false,
      createdateList: [],
      productOption: [],
      DialogVisiblejj: false,
      formLabelWidth: "110px",
      form: {
        type: "",
        explain: "",
        Adlist: ""
      },
      rules: {
        explain: [{ required: true, message: "请填写备注", trigger: "blur" }],
        type: [{ required: true, message: "请选择广告位", trigger: "blur" }],
        Adlist: [{ required: true, message: "请选择商盟广告", trigger: "blur" }]
      },
      param: {
        banner: {
          name: "", // 优惠券名称
          type: "", //类型
          createTime: "", // 创建开始日期
          createTime2: "" //
        },
        queryRequest: {
          pageNum: 1,
          pageSize: 10
        }
      },
      isVisible: false,
      isVisible1: false,
      tableData: [],
      total: 1,
      adrow: {},
      productOptions: [
        { id: "1", title: "首页banner广告位" },
        { id: "2", title: "首页推广位" },
        { id: "3", title: "订单支付完成后页面广告位" },
        { id: "4", title: "我的页面底部广告位" },
        { id: "5", title: "开屏弹窗广告位" },
        { id: "6", title: "秒杀广告位" },
        { id: "7", title: "拼团广告位" },
        { id: "8", title: "打包一口价广告位" },
        // { id: "9", title: "优惠券广告" }
      ]
    };
  },
  components: {
    "add-ad": AddAd,
    updateAd
  },
  mounted() {
    this.getbannerList();
  },

  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  methods: {
    handleClose() {},
    currentPageChange(e) {
      this.param.queryRequest.pageNum =e;
      this.getbannerList();
    },
    resetForm(formName) {
      this.DialogVisiblejj = false;
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        let message = this.form;
        if (valid) {
          // let param = {
          //   data: {
          //     id: this.cellData.id,
          //     appuerId: "1",
          //     status: "2",
          //     appReult: message.explain
          //   }
          // };
          // this.$service.tAgentIsApproval(param).then(res => {
          //   if (res.code == "00000") {
          //     this.$message({
          //       message: "成功",
          //       type: "success"
          //     });
          //     this.DialogVisiblejj = false;
          //     // this.$refs[formName].resetFields();
          //     this.initData();
          //   } else {
          //     this.$message({
          //       message: res.message,
          //       type: "warning"
          //     });
          //   }
          // });
        }
      });
    },
    search() {
      this.param.queryRequest.pageNum = 1;
      this.getbannerList();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.banner.name = "";
      this.param.banner.type = "";
      this.param.banner.createTime = "";
      this.param.banner.createTime2 = "";
      this.createdateList = [];
    },
    addAlliance() {
      this.DialogVisiblejj = true;
    },
    hadleAddUser() {
      this.isVisible = true;
    },
    getbannerList() {
      this.isLoading = true;
      if (this.createdateList.length > 0) {
        this.param.banner.createTime = this.createdateList[0];
        this.param.banner.createTime2 = this.createdateList[1];
      }
      this.$service.tbBannerQueryList(this.param).then(res => {
        this.isLoading = false;

        if (res.code == "00000") {
          if (
            res.data.rows.length == 0 &&
            this.param.queryRequest.pageNum != 1
          ) {
            this.param.queryRequest.pageNum -= 1;
            this.getbannerList();
          }
          for (let i = 0; i < res.data.rows.length; i++) {
            const element = res.data.rows[i];
            if (element.type == "1") {
              element.typesName = "首页banner广告位";
            } else if (element.type == "2") {
              element.typesName = "首页推广位";
            } else if (element.type == "3") {
              element.typesName = "订单支付完成后页面广告位";
            } else if (element.type == "4") {
              element.typesName = "我的页面底部广告位";
            } else if (element.type == "5") {
              element.typesName = "开屏弹窗广告位";
            } else if (element.type == "6") {
              element.typesName = "秒杀广告位";
            } else if (element.type == "7") {
              element.typesName = "拼团广告位";
            } else if (element.type == "8") {
              element.typesName = "打包一口价广告位";
            } else if (element.type == "9") {
              element.typesName = "优惠券广告";
            }
          }
          this.tableData = res.data.rows;
          this.total = res.data.total;
        }
      });
    },
    submitevent() {
      this.isVisible = false;
      this.getbannerList();
    },
    handleConfirmDelete(row) {
      this.$service.tbBannerDelete(row.id).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.getbannerList();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    handleClick(row) {
      this.adrow = JSON.parse(JSON.stringify(row));
      this.isVisible1 = true;
    },
    updateEvent() {
      this.isVisible1 = false;
      this.getbannerList();
    }
  }
};
</script>

<style lang="scss" scoped>
.banner-image {
  width: 150px;
}
</style>